/**微信小游戏平台 */

import { IPlatform } from "./IPlatform";
import { PlatformType } from "./PlatformType";


export default class WxPlatform implements Partial<IPlatform> {

    platType: number = PlatformType.Wechat;

    appId: string = '';


    constructor() {

    }

    /**
     * 登录code小于0表示失败. code等于1登录未过期 code等于0登录成功
     * @returns {Promise<{code: number, data: any}>}
     */
    login() {
        return new Promise<{ code: number, data: any }>((resolve, reject) => {

            wx.checkSession({
                success(res) {
                    resolve({
                        code: 1,
                        data: res
                    });
                },
                fail() {
                    wx.login({
                        success(res) {
                            resolve({
                                code: 0,
                                data: res
                            });
                        },
                        fail(res) {
                            resolve({
                                code: -1,
                                data: res
                            })
                        },
                        timeout: 3000
                    })
                }
            })

        })
    }

    /**拉起充值 */
    pay(payinfo: {
        payid: number,
        price: number,

    }) {

        return new Promise((resolve, reject) => {
            wx.requestMidasFriendPayment({
                buyQuantity: 30,
                currencyType: "CNY",
                platform: "android",
                env: 1,
                mode: "game",
                nonceStr: "",
                offerId: "1",
                outTradeNo: "1",
                signature: "",
                timeStamp: Date.UTC(1970, 0, 1, 0, 0, 0, 0),
                zoneId: "",
                success: function (res) {
                    console.log(res);
                    resolve(res);
                },
                fail: function (res) {
                    console.log(res);
                    reject(res);
                },
                complete: function (res) {
                    console.log(res);
                }

            })
        })

    }


    onHide(cb: Function) {
        wx.onHide((res) => {
            cb(res);
        });
    }

    onShow(cb: Function): void {
        wx.onShow((res) => {
            cb(res);
        });
    }

}